package com.baosight.sczx.zm.cs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.VariableGroup;
import com.baosight.sczx.zm.cs.domain.ZMCSVariableGroup;

/**
 * 变量组
 * <p>Title: ServiceZMCS44.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-2 下午12:14:34  严洪浩  Create
 *</pre>
 */
public class ServiceZMCS44 extends ServiceEPBase {

	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:31:31 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		return outInfo;
	}

	/**
	 * 查询变量组信息方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:31:46 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdParentId = inInfo.getString("fdParentId");
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCSVariableGroup.query", new ZMCSVariableGroup());				
		return outInfo;
	}
	
	/**
	 * 新增变量组数据
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:32:09 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		boolean validate = validate(inInfo, "ZMCSVariableGroup.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CI_VARIABLEGROUP"));//主键
			EiInfo outinfo = super.insert(inInfo, "ZMCSVariableGroup.insert", "result");
			return outinfo;
		}
	}
	
	/**
	 * 修改变量组数据
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:32:25 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validateValue = validate(inInfo, "ZMCSVariableGroup.validate", params);
		if(validateValue) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCSVariableGroup.update");
			return outInfo;
		}
	}

	/**
	 * 删除变量组数据
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:32:36 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++){
			_list.add(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		params.put("fdKeyIds", _list);
		
		//有无子节点校验
		boolean validate = VariableGroup.hasChildren(params, inInfo);
		if(validate) {
			return inInfo;
		}
		else {
			EiInfo _temp = super.delete(inInfo, "ZMCSVariableGroup.delete");
	        outInfo = query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}
	
	/**
	 * 使用状态为启用
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15上午11:26:05 张志芳
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSVariableGroup.modifyStart");//修改使用状态为启用
        EiInfo outInfo = query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 使用状态为停用
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15上午11:26:05 张志芳
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSVariableGroup.modifyStop");//修改使用状态为停用
        EiInfo outInfo = query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 对编号和名称校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15上午11:25:35 张志芳
	 *    2011-3-17下午16:25:30张小飞   修改校验的条件，即增加过滤条件fdProductionUnitsId
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdName = inInfo.getBlock("result").getCellStr(0, "fdName");
		String fdNo = inInfo.getBlock("result").getCellStr(0, "fdNo");
		String fdProductionUnitsId = inInfo.getBlock("result").getCellStr(0, "fdProductionUnitsId");
		
		params.put("fdName", fdName);
		params.put("fdNo", fdNo);
		params.put("fdProductionUnitsId", fdProductionUnitsId);
		
		List result = dao.query("ZMCSVariableGroup.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSVariableGroup _temp = (ZMCSVariableGroup)result.get(0);
			
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)) {
				msg.append(" 编号");
			}
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			msg.append(" 重复！(注: 与其他生产单元下的编号也不能重复) ");
			
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}

}
